#include "DiskScheduler.hpp"
#include <string>
#include <sstream>
#include <limits>


/**
 * @test 复制粘贴用于测试用例
 * 53
 * 98 183 37 122 14 124 65 67
 */



// 函数：打印帮助信息
// Function: Prints help information
void print_help() {
    std::cout << "\n--- Disk Scheduling Simulator ---\n";
    std::cout << "Commands:\n";
    std::cout << "  run    - Start a new simulation.\n";
    std::cout << "  help   - Show this help message.\n";
    std::cout << "  exit   - Exit the program.\n";
    std::cout << "---------------------------------\n";
}

// 函数：从用户输入中读取一个整数
// Function: Reads an integer from user input
int get_integer_input(const std::string& prompt) {
    int value;
    while (true) {
        std::cout << prompt;
        std::cin >> value;
        if (std::cin.good()) {
            // 清除输入缓冲区中剩余的内容
            // Clear the rest of the input buffer
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            return value;
        }
        std::cout << "Invalid input. Please enter an integer." << std::endl;
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
}

// 函数：运行一次完整的模拟
// Function: Runs a full simulation
void run_simulation() {
    // 1. 获取初始磁头位置
    // 1. Get initial head position
    int start_pos = get_integer_input("Enter initial head position: ");

    // 2. 获取磁道请求序列
    // 2. Get the track request sequence
    std::vector<int> requests;
    std::string line;
    std::cout << "Enter track requests (space-separated, e.g., 98 183 37 122): ";
    std::getline(std::cin, line);
    std::stringstream ss(line);
    int track;
    while (ss >> track) {
        requests.push_back(track);
    }
    
    if (requests.empty()) {
        std::cout << "Warning: Request queue is empty. Nothing to simulate.\n";
    }

    // 3. 创建调度器实例
    // 3. Create a scheduler instance
    DiskScheduler scheduler(start_pos, requests);

    // 4. 选择并运行算法
    // 4. Choose and run an algorithm
    while (true) {
        std::cout << "Choose algorithm (1: SSTF, 2: LOOK, 0: Back): ";
        int choice;
        std::cin >> choice;
        if (!std::cin.good()) {
            choice = -1; // Force invalid choice
        }
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

        if (choice == 1) {
            scheduler.runSSTF();
        } else if (choice == 2) {
            scheduler.runLOOK();
        } else if (choice == 0) {
            break;
        } else {
            std::cout << "Invalid choice. Please try again." << std::endl;
        }
    }
}


int main() {
    print_help();
    std::string command;
    while (true) {
        std::cout << "\n> ";
        std::cin >> command;
        // 清理输入缓冲区
        // Clear input buffer
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

        if (command == "run") {
            run_simulation();
        } else if (command == "help") {
            print_help();
        } else if (command == "exit") {
            break;
        } else {
            std::cout << "Unknown command. Type 'help' for a list of commands." << std::endl;
        }
    }
    std::cout << "Exiting simulator." << std::endl;
    return 0;
}
